<?php
/**
* 
*/
class IndexModel extends MY_Model
{

	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_user';
	}


	//统计信息
	public function count_data()
	{
		//今日营收
		$day = date('Y-m-d 00:00:00');
		//本月消费
		$start = date('Y-m-01 00:00:00');
		$end = date('Y-m-d H:i:s');

		$query = $this->db->query("SELECT SUM(price) as day_total FROM nmp_user_card_log WHERE addtime >= unix_timestamp('$day') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['day_total'] = $res['day_total'] ? $res['day_total'] : 0;

		$query = $this->db->query("SELECT SUM(price) as month_total FROM nmp_user_card_log WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['month_total'] = $res['month_total'] ? $res['month_total'] : 0;

		//今日会员
		$query = $this->db->query("SELECT count(*) as duser_num FROM nmp_user WHERE addtime >= unix_timestamp('$day') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['duser_num'] = $res['duser_num'] ? $res['duser_num'] : 0;

		$query = $this->db->query("SELECT count(*) as muser_num FROM nmp_user WHERE addtime >= unix_timestamp('$start') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['muser_num'] = $res['muser_num'] ? $res['muser_num'] : 0;

		//今日预约 distinct(user_id)
		$query = $this->db->query("SELECT count(*) as appoint_num FROM nmp_user_appoint WHERE addtime >= unix_timestamp('$day') AND addtime <= unix_timestamp('$end')");
		$res = $query->row_array();
		$data['appoint_num'] = $res['appoint_num'] ? $res['appoint_num'] : 0;

		//预约率
		$query = $this->db->query("SELECT count(*) as user_num FROM nmp_user");
		$res = $query->row_array();
		$data['app_percen'] = round($data['appoint_num']/$res['user_num']*100,1)."％";

		return $data;
	}



	//获取最新办卡
	public function card_data()
	{
		$query = $this->db->query("SELECT uc.total_num, uc.addtime, u.headimg, co.name as course_name, c.card_name FROM nmp_user_card as uc 
			LEFT JOIN nmp_course_card as c ON uc.card_id = c.id 
			JOIN nmp_user as u ON uc.user_id = u.id 
			JOIN nmp_course as co ON uc.cid = co.id ORDER BY uc.addtime DESC LIMIT 0, 5");
		$res = $query->result_array();

		foreach ($res as $key => $value) {
			$res[$key]['addtime'] = date('m-d H:i', $value['addtime']);
		}

		return $res;
	}



	//存储公告
	public function storage_notice($arr)
	{
		$arr['addtime'] = time();
		if ($this->db->insert('nmp_notice',$arr)) 
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	//获取公告
	public function notice_data()
	{
		$query = $this->db->query("SELECT id, notice_title, addtime, content FROM nmp_notice ORDER BY addtime DESC");
		$data = $query->result_array();

		foreach ($data as $key => $value) {
			$data[$key]['addtime'] = date('m-d',$value['addtime']);
		}

		return $data;
	}


	
}





?>